package com.xilihui.xlh.business.activitys;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

import com.xilihui.xlh.R;
import com.xilihui.xlh.business.requests.MineRequest;
import com.xilihui.xlh.core.app.BaseCompatActivity;
import com.xilihui.xlh.core.app.BaseEntity;
import com.xilihui.xlh.core.app.ToolBaseCompatActivity;
import com.xilihui.xlh.core.http.DoTransform;
import com.xilihui.xlh.core.http.HttpSubscriber;
import com.xilihui.xlh.core.util.ToastUtil;
import com.xilihui.xlh.core.util.YEventBuses;

import org.feezu.liuli.timeselector.Utils.TextUtil;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/7/4.
 */

public class EditActivity extends ToolBaseCompatActivity{
    
    @BindView(R.id.tv_num)
    TextView tv_num;
    @BindView(R.id.et_content)
    EditText et_content;
    int type = 1;
    int leng = 0;
    String typeStr = "";
    
    @Override
    public int getLayout() {
        return R.layout.activity_edit;
    }
    
    @OnClick(R.id.iv_toolbar_left)
    public void back(){
        finish();
    }
    
    @OnClick(R.id.tv_toolbar_right)
    public void save(){
        doSave();
    }

    @Override
    public String initTitle() {
        return null;
    }

    @Override
    public void initUI(@Nullable Bundle savedInstanceState) {
        
        type = getIntent().getIntExtra("type",1);
        if (type==1){
            setToolTitle("昵称");
            typeStr = "nickname";
            leng = 10;
            et_content.setMaxHeight(10);
            tv_num.setText(10+"");
        }else {
            setToolTitle("个性签名");
            typeStr = "signature";
            leng = 20;
            et_content.setMaxHeight(20);
            tv_num.setText(20+"");
        }
        setToolRightText("保存");
        
        et_content.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                int num = leng-charSequence.length();
                tv_num.setText(num+"");
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        
    }

    @Override
    public void initData() {

    }

    /**
     * 保存
     */
    public void doSave(){
        
        String field = et_content.getText().toString().trim();
        if(TextUtil.isEmpty(field)){
            if (type==1){
                ToastUtil.toastShortNegative("请输入呢称");
            }else {
                ToastUtil.toastShortNegative("请输入个性签名");
            }
            
        }
        
        MineRequest.userSave(EditActivity.this,typeStr,field)
                .compose(DoTransform.<BaseEntity>applyScheduler(this,true))
                .compose(EditActivity.this.<BaseEntity>bindToLifecycle())
                .subscribe(new HttpSubscriber<BaseEntity>(this) {
                    @Override
                    protected void onSuccess(BaseEntity entity) {
                        ToastUtil.toastShortPositive(entity.getMsg());
                        YEventBuses.post(new YEventBuses.Event("sx"));
                        finish();
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.toastShortNegative(msg);
                    }
                });
    }
    
    
}
